--=======================================
--[[ 本地函数 ]]
--=======================================
-- 移除预制物(预制物,数量)
local function removeItem(item, num)
    if item.components.stackable then
        item.components.stackable:Get(num):Remove()
    else
        item:Remove()
    end
end
--=======================================
--[[ 动作定义 ]]
--=======================================
local actions = {
    BBOY_UPGRADE = {
        id = "BBOY_UPGRADE",
        priority = 6,
        strfn = function(act)
            if act.target and act.target:HasTag("wall") then
                return "CHANGE"
            elseif act.target and act.target:HasTag("lamp") then
                return "CHANGE"
            end
            return "GENERIC"
        end,
        fn = function(act)
            local target = act.target
            local invobject = act.invobject
            if target.prefab == "wall_bboy_courtyard" then
                local new_wall = nil
                new_wall = SpawnPrefab("wall_bboy_nether_blossom")

                local x, y, z = target.Transform:GetWorldPosition()
                new_wall.Transform:SetPosition(x, y, z)
                -- local c_health = target.components.health and target.components.health.currenthealth
                -- new_wall.components.health:SetCurrentHealth(c_health)

                target:Remove()
                removeItem(invobject, 1)

                local fx = SpawnPrefab("chestupgrade_stacksize_fx")
                fx.Transform:SetPosition(target.Transform:GetWorldPosition())

                return true
            elseif target.prefab == "bboy_candle01" then
                local new_candle = nil
                new_candle = SpawnPrefab("bboy_candle02")

                local x, y, z = target.Transform:GetWorldPosition()
                new_candle.Transform:SetPosition(x, y, z)

                target:Remove()
                removeItem(invobject, 1)

                return true
            elseif target.prefab == "bboy_candle02" then
                local new_candle = nil
                new_candle = SpawnPrefab("bboy_candle03")

                local x, y, z = target.Transform:GetWorldPosition()
                new_candle.Transform:SetPosition(x, y, z)

                target:Remove()
                removeItem(invobject, 1)

                return true
            elseif target.prefab == "bboy_candle_lamp" then
                local new_lamp = nil
                new_lamp = SpawnPrefab("bboy_nether_lamp")

                local x, y, z = target.Transform:GetWorldPosition()
                new_lamp.Transform:SetPosition(x, y, z)

                target:Remove()
                removeItem(invobject, 1)

                local fx = SpawnPrefab("chestupgrade_stacksize_fx")
                fx.Transform:SetPosition(target.Transform:GetWorldPosition())

                return true
            end
        end,
    },
    BBOY_DESTINATION_UI = {
        id = "BBOY_DESTINATION_UI",
        priority = 1,
        strfn = function(act)
            return "GENERIC"
        end,
        -- mount_valid = true,
        encumbered_valid = true,
        fn = function(act)
            if act.doer ~= nil and act.target ~= nil and act.doer:HasTag("player") and
                act.target.components.bboy_travelable and not act.target:HasTag("burnt") and
                not act.target:HasTag("fire") then
                act.target.components.bboy_travelable:BeginTravel(act.doer)
                return true
            end
        end,
    },
    BBOY_TOUCH = {
        id = "BBOY_TOUCH",
        priority = 6,
        strfn = function(act)
            return "GENERIC"
        end,
        fn = function(act)
            local target = act.target
            if target ~= nil and target.components.bboy_touch then
                target.components.bboy_touch:BeginTouch(act.doer)
                return true
            end
        end,
    },
    BBOY_READ = {
        id = "BBOY_READ",
        -- mount_valid = true,
        strfn = function(act)
            return "GENERIC"
        end,
        fn = function(act)
            local target = act.target or act.invobject
            if target ~= nil and act.doer ~= nil then
                local success, reason = target.components.bboy_multi_teacher:Teach(act.doer)
                return success, reason
            end
        end,
    },
    BBOY_WORSHIP = {
        id = "BBOY_WORSHIP",
        -- mount_valid = true,
        strfn = function(act)
            return "GENERIC"
        end,
        fn = function(act)
            local target = act.target
            local invobject = act.invobject
            if target == nil or invobject == nil then
                return false
            end
            if target.components.bboy_worship then
                if target.show_item ~= nil then
                    return false, "ALREADY_OFFERED"
                end
                target.components.bboy_worship:SetShow(target, invobject, true, act.doer)
                if invobject.components.stackable then
                    removeItem(invobject, 1)
                end
                return true
            else
                return false
            end
        end,
    },
}

for _, action in pairs(actions) do
    local _action = Action()
    for k, v in pairs(action) do
        _action[k] = v
    end
    AddAction(_action)
end

--=======================================
--[[ 动作行为:就是执行动作的人物动画 ]]
--=======================================
AddStategraphActionHandler("wilson", ActionHandler(ACTIONS.BBOY_UPGRADE, "dolongaction"))
AddStategraphActionHandler("wilson_client", ActionHandler(ACTIONS.BBOY_UPGRADE, "dolongaction"))
AddStategraphActionHandler("wilson", ActionHandler(ACTIONS.BBOY_DESTINATION_UI, "give"))
AddStategraphActionHandler("wilson_client", ActionHandler(ACTIONS.BBOY_DESTINATION_UI, "give"))
AddStategraphActionHandler("wilson", ActionHandler(ACTIONS.BBOY_TOUCH, "give"))
AddStategraphActionHandler("wilson_client", ActionHandler(ACTIONS.BBOY_TOUCH, "give"))
AddStategraphActionHandler("wilson", ActionHandler(ACTIONS.BBOY_READ, "bboy_book_peruse"))
AddStategraphActionHandler("wilson_client", ActionHandler(ACTIONS.BBOY_READ, "bboy_book_peruse"))
AddStategraphActionHandler("wilson", ActionHandler(ACTIONS.BBOY_WORSHIP, "give"))
AddStategraphActionHandler("wilson_client", ActionHandler(ACTIONS.BBOY_WORSHIP, "give"))

--=======================================
-- [[ ADD COMPONENT ACTION ]]
-- SCENE		using an object in the world                                        -- args: inst, doer, actions, right
-- USEITEM		using an inventory item on an object in the world                   -- args: inst, doer, target, actions, right
-- POINT		using an inventory item on a point in the world                     -- args: inst, doer, pos, actions, right, target
-- EQUIPPED		using an equiped item on yourself or a target object in the world   -- args: inst, doer, target, actions, right
-- INVENTORY	using an inventory item                                             -- args: inst, doer, actions, right
--=======================================

-- target是对象,inst是手中或鼠标上物品,doer是做动作的玩家,right是是否为右键
AddComponentAction("USEITEM", "bboy_upgrade", function(inst, doer, target, actions, right)
    if target.prefab == "wall_bboy_courtyard" and inst:HasTag("bboy_nether_petal") then
        table.insert(actions, ACTIONS.BBOY_UPGRADE)
    end
    if (target.prefab == "bboy_candle01" or target.prefab == "bboy_candle02") and inst:HasTag("bboy_candle") then
        table.insert(actions, ACTIONS.BBOY_UPGRADE)
    end
    if target.prefab == "bboy_candle_lamp" and inst:HasTag("bboy_nether_petal") then
        table.insert(actions, ACTIONS.BBOY_UPGRADE)
    end
end)

AddComponentAction("INVENTORY", "inventoryitem", function(inst, doer, actions, right)
    if inst.prefab == "bboy_book_survival_guide" and inst:HasTag("book_survival_guide") then
        table.insert(actions, ACTIONS.BBOY_READ)
    end
end)

AddComponentAction("USEITEM", "inventoryitem", function(inst, doer, target, actions, right)
    if target.prefab == "bboy_lucky_cat" then
        table.insert(actions, ACTIONS.BBOY_WORSHIP)
    end
end)

AddComponentAction("SCENE", "bboy_travelable", function(inst, doer, actions, right)
    if right then
        if not inst:HasTag("burnt") and not inst:HasTag("fire") then
            table.insert(actions, ACTIONS.BBOY_DESTINATION_UI)
        end
    end
end)

AddComponentAction("SCENE", "bboy_touch", function(inst, doer, actions, right)
    if right and inst:HasTag("bboy_nether_lamp") then
        table.insert(actions, ACTIONS.BBOY_TOUCH)
    end
end)

-- 动作兼容行为排队论
local queueractlist = {
    BBOY_UPGRADE = "allclick",
}
local actionqueuer_status, actionqueuer_data = pcall(require, "components/actionqueuer")
if actionqueuer_status then
    if AddActionQueuerAction and next(queueractlist) then
        for k, v in pairs(queueractlist) do
            AddActionQueuerAction(v, k, true)
        end
    end
end

-- 先创建动作再命名
STRINGS.ACTIONS.BBOY_UPGRADE = {
    GENERIC = STRINGS.BBOY_ACTIONS.BBOY_UPGRADE.GENERIC,
    CHANGE = STRINGS.BBOY_ACTIONS.BBOY_UPGRADE.CHANGE,
}
-- STRINGS.CHARACTERS.GENERIC.ACTIONFAIL.BBOY_UPGRADE = {
--     ALREADY_UPGRADED = STRINGS.BBOY_ACTIONS.BBOY_UPGRADE.ACTIONFAIL.ALREADY_UPGRADED,
-- }

STRINGS.ACTIONS.BBOY_DESTINATION_UI = {
    GENERIC = STRINGS.BBOY_ACTIONS.BBOY_DESTINATION_UI.GENERIC,
}

STRINGS.ACTIONS.BBOY_TOUCH = {
    GENERIC = STRINGS.BBOY_ACTIONS.BBOY_TOUCH.GENERIC,
}

STRINGS.ACTIONS.BBOY_READ = {
    GENERIC = STRINGS.BBOY_ACTIONS.BBOY_READ.GENERIC,
}
STRINGS.CHARACTERS.GENERIC.ACTIONFAIL.BBOY_READ = {
    NOT_BUILDER = STRINGS.BBOY_ACTIONS.BBOY_READ.ACTIONFAIL.NOT_BUILDER,
    ALL_KNOWN = STRINGS.BBOY_ACTIONS.BBOY_READ.ACTIONFAIL.ALL_KNOWN,
}

STRINGS.ACTIONS.BBOY_WORSHIP = {
    GENERIC = STRINGS.BBOY_ACTIONS.BBOY_WORSHIP.GENERIC,
}
STRINGS.CHARACTERS.GENERIC.ACTIONFAIL.BBOY_WORSHIP = {
    ALREADY_OFFERED = STRINGS.BBOY_ACTIONS.BBOY_WORSHIP.ACTIONFAIL.ALREADY_OFFERED,
}
